﻿using DryIoc;
using DryIoc.Microsoft.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using Sany.AGV.RobotTool.ViewModels;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Threading;

namespace Sany.AGV.RobotTool
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        public App()
        {
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
        }

        protected override IContainerExtension CreateContainerExtension()
        {
            var serviceCollection = new ServiceCollection();
            //serviceCollection.AddSingleton<ITestService, TestService>();
            return new DryIocContainerExtension(new Container(CreateContainerRules()).WithDependencyInjectionAdapter(serviceCollection));
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            return base.CreateModuleCatalog();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //containerRegistry.RegisterForNavigation<Views.MainWindow, MainWindowViewModel>();
        }

        protected override Window CreateShell()
        {
            return Container.Resolve<Views.MainWindow> ();
        }

        protected override void OnInitialized()
        {

            base.OnInitialized();
        }

        /// <summary>
        /// 程序退出
        /// </summary>
        /// <param name="e"></param>
        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);

            Environment.Exit(0);
        }

        /// <summary>
        /// 激活
        /// </summary>
        /// <param name="e"></param>
        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
        }

        /// <summary>
        /// 反激活
        /// </summary>
        /// <param name="e"></param>
        protected override void OnDeactivated(EventArgs e)
        {
            base.OnDeactivated(e);
        }


        /// <summary>
        /// UI线程异常捕获
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
        }

        /// <summary>
        /// 非UI线程异常捕获
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            StringBuilder stringBuilder = new StringBuilder();
            if (e.IsTerminating)
            {
                stringBuilder.AppendLine("程序AppDomain.CurrentDomain发生错误！");
            }
            stringBuilder.Append("捕获未处理异常：");
            if (e.ExceptionObject is Exception)
            {
                stringBuilder.Append(((Exception)e.ExceptionObject).ToString());
            }
            else
            {
                stringBuilder.Append(e.ExceptionObject.ToString());
            }

            //重启
            if (e.IsTerminating)
            {
                string? application = Process.GetCurrentProcess()?.MainModule?.FileName;
                Thread thread = new Thread(new ParameterizedThreadStart(s =>
                {
                    Thread.Sleep(300);
                    Process pcs = new Process();
                    pcs.StartInfo.FileName = s.ToString();
                    pcs.Start();
                }));
                thread.Start(application);
            }
        }

        /// <summary>
        /// Task线程异常捕获
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
        {
            e.SetObserved();
        }

    }
}
